//
//  SY_Setting_PassWordViewController.m
//  ChatDemo-UI2.0
//
//  Created by 苏楠 on 16/8/18.
//  Copyright © 2016年 苏楠. All rights reserved.
//

#import "SY_Setting_PassWordViewController.h"
#import "SY_Setting_MakeSurePhoneNumViewController.h"

@interface SY_Setting_PassWordViewController ()
{
    UIButton *_nextBtn;
    
    UITextField *_passWordTextField;
}

@end

@implementation SY_Setting_PassWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navBar.nav_titleLabel.text = @"身份验证";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view setBackgroundColor:RGBCOLOR(240,239,245)];
    
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 12+SafeAreaTopHeight, SCREENWIDTH, 46)];
    
    [backView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:backView];
    
    UILabel *leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(12, 0, 70, 46)];
    leftLabel.text = @"登录密码";
    [leftLabel setFont:[UIFont systemFontOfSize:15]];
    [leftLabel setTextColor:RGBCOLOR(59, 59, 59)];
    
    [backView addSubview:leftLabel];
    
    _passWordTextField = [[UITextField alloc]initWithFrame:CGRectMake(82, 0, 200, 46)];
    [_passWordTextField setFont:[UIFont systemFontOfSize:16]];
    _passWordTextField.placeholder = @"请输入登录密码";
    _passWordTextField.secureTextEntry = YES;
    [_passWordTextField setTextColor:RGBCOLOR(59, 59, 59)];
    [_passWordTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    //_passWordTextField.delegate = self;
    [backView addSubview:_passWordTextField];
    
    _nextBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [_nextBtn setFrame:CGRectMake(12, 110 + SafeAreaTopHeight, SCREENWIDTH - 24, 38)];
    _nextBtn.layer.cornerRadius = 5;
    [_nextBtn setTitle:@"下一步" forState:(UIControlStateNormal)];
    [_nextBtn setTintColor:[UIColor whiteColor]];
    [_nextBtn setBackgroundColor:DEFAULTCOLOR];
    [_nextBtn addTarget:self action:@selector(nextBtnAction) forControlEvents:(UIControlEventTouchUpInside)];
    _nextBtn.enabled = NO;
    _nextBtn.alpha = 0.5;
    [self.view addSubview:_nextBtn];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)nextBtnAction {
   
    if ([[_passWordTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0) {
        
        [Utils alertViewWithMessage:@"请输入密码"];
        
        return;
    }
    
    if (_passWordTextField.text.length<6 ||_passWordTextField.text.length>16) {
        [Utils alertViewWithMessage:@"请将密码限制在6-16位以内"];
        return;
    }
    
    NSMutableDictionary * dict = [[NSMutableDictionary alloc] initWithCapacity:0];
    [dict setObject:@"api_user_login" forKey:@"method"];
    [dict setObject:[SYAccountTool ShareAccount].account.mobile forKey:@"loginname"];
    
    NSString * finalPassWord = [Utils MD5EncryptWith:_passWordTextField.text];
    [dict setObject:finalPassWord forKey:@"userpass"];
    
    [Utils get:nil parameters:dict success:^(NSInteger recode, id resposeJson){
        NSLog(@"%@", resposeJson);

        [self->_passWordTextField resignFirstResponder];
        SY_Setting_MakeSurePhoneNumViewController *msVC = [[SY_Setting_MakeSurePhoneNumViewController alloc]init];
        msVC.typeNum = @"2";
        [self.navigationController pushViewController:msVC animated:YES];
        
    } failure:^(NSInteger recode, NSError * error){
    }];
    
}


- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField == _passWordTextField) {
        
        
        if (_passWordTextField.text.length > 16) {
            UITextRange *markedRange = [_passWordTextField markedTextRange];
            if (markedRange) {
                return;
            }
            //Emoji占2个字符，如果是超出了半个Emoji，用15位置来截取会出现Emoji截为2半
            //超出最大长度的那个字符序列(Emoji算一个字符序列)的range
            NSRange range = [_passWordTextField.text rangeOfComposedCharacterSequenceAtIndex:16];
            _passWordTextField.text = [_passWordTextField.text substringToIndex:range.location];
        }
        
        
        if (textField.text.length >= 6 &&textField.text.length <= 16) {
            _nextBtn.enabled = YES;
            _nextBtn.alpha = 1;
        }else{
            _nextBtn.enabled = NO;
            _nextBtn.alpha = 0.5;
        }
    }
}


@end
